﻿Imports System.Windows.Forms
Imports ArfaUI

Public Class LoginPage
    Inherits PageTemplate

    Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click, Button1.Click
        If My.Settings.dbname = "" Then
            MessageBox.Show("Maaf, database belum didefinisikan. Silahkan definisikan terlebih dahulu")
            Return
        End If
        If UserNameTextBox.Text <> "" Then
            If PasswordTextBox.Text <> "" Then
                Dim dt As New DataTable
                If Utils.executeSP("proc_petugas", New Object() {"selectlogin", UserNameTextBox.Text, PasswordTextBox.Text, "0", "0", "0", "0"}, dt) Then
                    If dt.Rows.Count > 0 Then
                        'login berhasil, hapus history view
                        MainWindow.history.Clear()
                        Session.username = dt.Rows(0)("username") 'or the usernameTextBox
                        'Session.vusername = dt.Rows(0)("namalengkap")
                        Session.password = dt.Rows(0)("pass")
                        Session.kodeRole = dt.Rows(0)("koderole")
                        LocalSession.operatorname = dt.Rows(0)("username")
                        LocalSession.operatorrole = dt.Rows(0)("role")
                        'TOFIX : ini spy cepet aja
                        Dim dtN As New DataTable
                        If Utils.executeSP("proc_master_profil", New Object() {"select", Convert.DBNull, Convert.DBNull, Convert.DBNull, Convert.DBNull}, dtN) Then
                            If dtN IsNot Nothing Then
                                If dtN.Rows.Count > 0 Then
                                    LocalSession.namaresto = dtN.Rows(0)("nama")
                                    LocalSession.alamatresto = dtN.Rows(0)("alamat")
                                    LocalSession.telpresto = dtN.Rows(0)("telepon")
                                    LocalSession.faxrest = dtN.Rows(0)("fax")
                                Else
                                    LocalSession.namaresto = ""
                                    LocalSession.alamatresto = ""
                                    LocalSession.telpresto = ""
                                    LocalSession.faxrest = ""
                                End If
                            Else
                                LocalSession.namaresto = ""
                                LocalSession.alamatresto = ""
                                LocalSession.telpresto = ""
                                LocalSession.faxrest = ""
                            End If
                        End If


                        LocalSession.isLoggedIn = True

                        UserNameTextBox.Text = ""
                        PasswordTextBox.Text = ""

                        MainWindow.setFirstNavigation(Pages.Item("mainmenu"))
                        'Select Case Session.kodeRole
                        '    Case 1
                        '        MainWindow.setFirstNavigation(Pages.Item("homeAdministrator"))
                        '    Case 2
                        '        MainWindow.setFirstNavigation(Pages.Item("homeFO"))
                        '    Case 3
                        '        MainWindow.setFirstNavigation(Pages.Item("kasirDokter"))
                        '    Case Else
                        '        MessageBox.Show("Peran tidak didefinisikan untuk user ini " & Session.kodeRole)
                        'End Select
                        'MainForm.roleid = dt.Rows(0).Item("koderole")
                    Else
                        MessageBox.Show("Username atau password salah. Silahkan coba lagi.")
                    End If
                End If
            Else
                MessageBox.Show("Password belum diisi.")
            End If
        Else
            MessageBox.Show("Username belum diisi.")
        End If
    End Sub
    
    Private Sub LoginPage_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        UserNameTextBox.Text = "a"
        PasswordTextBox.Text = "a"
    End Sub

    Private Sub PasswordTextBox_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles PasswordTextBox.KeyDown
        If e.KeyCode = Keys.Tab Then
            Button1.Focus()
        ElseIf e.KeyCode = Keys.Enter Then
            btnLogin_Click(sender, e)
        End If
    End Sub
End Class
